﻿using System;
using System.Linq;
using System.Web.Mvc;
using Blog.Core.Entities;
using Blog.Infrastructure.Configuration;
using Blog.Infrastructure.DataAcces.Services.Abstractisations;
using Blog.Presentation.Models.Dislpay;

namespace Blog.Presentation.Controllers
{
    public class ArticlesController : Controller
    {
        private readonly int articlesPerPage = 3;
        protected IArticleService articleService;
        private readonly ISettings _settings;

        public ArticlesController(IArticleService articleService, ISettings settings)
        {
            this.articleService = articleService;
            _settings = settings;
        }

        public ViewResult Index()
        {
            var model = articleService.GetLatest(3); 
            
            return View("Index", ArticleDto.FromAListOfArticles(model));
        }

        public PartialViewResult ShortArticlesView()
        {
            var model = articleService.GetLatest(3);
            return PartialView("ShortView", ArticleDto.FromAListOfArticles(model));
        }

        [Authorize]
        public ViewResult New()
        {
            return View(new Article());
        }

        [HttpPost]
        public RedirectToRouteResult New(Article article)
        {
            
            articleService.Save(article);

            return RedirectToAction("Index", "Home");
        }

        public RedirectToRouteResult Delete(long id)
        {
            articleService.Delete(id);
            return RedirectToAction("Index");
        }

        [Authorize]
        public ViewResult Edit(long id)
        {
            return View("Edit", articleService.Get(id));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public RedirectToRouteResult Edit(Article article)
        {
            article.Date = DateTime.Now;
            articleService.Save(article);
            return RedirectToAction("List");
        }

        public ViewResult View(string title)
        {
            var model = articleService.GetForView(title.Replace('-',' '));
            
            return View("FullView", model);
        }

        public ViewResult Archive(int index = 1)
        {
            var pages = articleService.PagesInArchive();
            var articles = articleService.Archive(index, articlesPerPage);

            return View("Archive",
                        new ArchiveDto
                            {
                                CurrentPage = index,
                                Articles = ArticleDto.FromAListOfArticles(articles),
                                Pages = pages
                            });
        }

        public ViewResult MyWorld(int index = 1)
        {
           
            var arts = articleService.ByCategory("Lumea Mea");

            var toDisplay = arts.Skip((index - 1) * articlesPerPage).Take(articlesPerPage);

            return View("MyWorld",
                        new ArchiveDto
                        {
                            CurrentPage = index,
                            Articles = ArticleDto.FromAListOfArticles(toDisplay),
                            Pages = arts.Count / articlesPerPage + arts.Count % articlesPerPage
                        });
        }


        public ViewResult CategoryView(string category)
        {
            ViewBag.Title = category;
            var model = articleService.ByCategory(category);

            return View(model);
        }

        [ChildActionOnly]
        public PartialViewResult Categories()
        {
            var model = articleService.Categories();

            return PartialView(model);
        }

        [ChildActionOnly]
        public PartialViewResult MostRecent(int count)
        {
            var model = articleService.GetLatest(count);

            return PartialView("Posts",model);
        }

        [ChildActionOnly]
        public PartialViewResult MostPopular(int count)
        {
            var model = articleService.GetMostPopular(count);

            return PartialView("Posts", model);
        }
    }
}
